﻿/*
COPYRIGHT (C) 2009 EPISERVER AB

THIS FILE IS PART OF DYNAMIC CONTENT CONTRIB FOR EPISERVER CMS.
DYNAMIC CONTENT CONTRIB FOR EPISERVER CMS IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

DYNAMIC CONTENT CONTRIB FOR EPISERVER CMS IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH DYNAMIC CONTENT CONTRIB FOR EPISERVER CMS. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiServer.DynamicContent;
using System.Web.UI;
using EPiServer.Core;
using EPiServer.SpecializedProperties;

namespace EPiServer.Research.DynamicContent
{
    public class ControlDC<ControlType> : DynamicContentBase where ControlType: Control
    {

        protected DCPlugin _att;

        protected List<System.Reflection.PropertyInfo> _propInfo;

        public ControlDC()
        {
            _att=(DCPlugin) typeof(ControlType).GetCustomAttributes(typeof(DCPlugin), true).First();
            properties = new PropertyDataCollection();
            _propInfo=typeof(ControlType)
                .GetProperties(System.Reflection.BindingFlags.Public| System.Reflection.BindingFlags.DeclaredOnly| System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.SetProperty | System.Reflection.BindingFlags.GetProperty)
                .Where(pi => pi.PropertyType.IsSubclassOf(typeof(PropertyData)) || (pi.PropertyType == typeof(string)) || (pi.PropertyType==typeof(bool)) || (pi.PropertyType == typeof(PageReference)) || (pi.PropertyType == typeof(int)))
                .ToList();

            foreach (System.Reflection.PropertyInfo pi in _propInfo)
            {
                if (pi.PropertyType.IsSubclassOf(typeof(PropertyData)))
                {
                    PropertyData pd = Activator.CreateInstance(pi.PropertyType) as PropertyData;
                    pd.Name = pi.Name;
                    properties.Add(pd);
                }
                else if (pi.PropertyType==typeof(string))
                {
                    PropertyString ps = new PropertyString();
                    ps.Name = pi.Name;
                    properties.Add(ps);
                }
                else if (pi.PropertyType == typeof(int))
                {
                    PropertyNumber pn = new PropertyNumber();
                    pn.Name = pi.Name;
                    properties.Add(pn);
                }
                else if (pi.PropertyType == typeof(bool))
                {
                    PropertyBoolean pb = new PropertyBoolean();
                    pb.Name = pi.Name;
                    properties.Add(pb);
                }
                else if (pi.PropertyType == typeof(PageReference))
                {
                    PropertyPageReference pp = new PropertyPageReference();
                    pp.Name = pi.Name;
                    properties.Add(pp);
                }
            }
        }

        #region IDynamicContent Members

        public override System.Web.UI.Control GetControl(PageBase hostPage)
        {
            Control c = hostPage.LoadControl(UriSupport.ResolveUrlBySettings(_att.Url));

            foreach (System.Reflection.PropertyInfo pi in _propInfo)
            {
                if (properties[pi.Name] != null)
                {
                    if (properties[pi.Name].Value != null)
                    {
                        if (pi.PropertyType.IsSubclassOf(typeof(PropertyData))) pi.SetValue(c, properties[pi.Name], null);
                        else pi.SetValue(c, properties[pi.Name].Value, null);
                    }
                }
            }

            return c;
        }

        public override EPiServer.Core.PropertyDataCollection Properties
        {
            get { return properties; }
        }

        public override string Render(PageBase hostPage)
        {
            throw new NotImplementedException();
        }

        public override bool RendersWithControl
        {
            get { return true; }
        }

        #endregion
    }
}
